import time

from utils.util import selectForOne


async def testFinishedEditor(browser):
    try:
        browser.find_element_by_xpath("//span[contains(text(),'库存管理')]").click()  # 点击导航
        time.sleep(0.5)
        menus = browser.find_elements_by_class_name('popover-menu')  # 所有菜单
        nowMenus = menus[len(menus) - 1]  # 获取最后一个菜单，点开的都回默认为最后一个
        # 点击第一排第一个
        nowMenus.find_elements_by_class_name('menu-prop__item')[0].find_elements_by_tag_name('a')[0].click()
        time.sleep(2)
        browser.find_element_by_class_name('bill-title').click()
        time.sleep(0.5)
        browser.find_element_by_xpath("//span[contains(text(),'新增')]").click()
        time.sleep(0.5)
        browser.find_element_by_xpath(
            "//span[contains(text(),'仓库:')]/following-sibling::div/div/input").click()
        time.sleep(0.5)
        selectForOne(browser)
        time.sleep(0.5)
        # 表格需要编辑的
        tableUpdateList = [{'columnName': '存货', 'index': 0, 'isFixed': False, 'td': ''},
                           {'columnName': '货位', 'index': 0, 'isFixed': False, 'td': ''}]
        # {'columnName': '备注', 'index': -1, 'isFixed': False}
        # 固定的表头找index
        header = browser.find_elements_by_class_name('el-table__fixed-header-wrapper')
        for item in header:
            for index, item2 in enumerate(item.find_elements_by_tag_name('th')):
                for item3 in tableUpdateList:
                    if item3['columnName'] == item2.text:
                        item3['index'] = index
                        item3['isFixed'] = True
        time.sleep(0.5)
        # 正常的表头找index
        header = browser.find_elements_by_class_name('el-table__header-wrapper')
        for item in header:
            for index, item2 in enumerate(item.find_elements_by_tag_name('th')):
                for item3 in tableUpdateList:
                    if item3['columnName'] == item2.text:
                        item3['index'] = index
                        item3['isFixed'] = False

        # 固定表格找td
        table = browser.find_element_by_class_name('el-table__fixed-body-wrapper')
        tableTr = table.find_element_by_tag_name('tbody').find_element_by_tag_name('tr')
        for index, item in enumerate(tableTr.find_elements_by_tag_name('td')):
            for item2 in tableUpdateList:
                if index == item2['index'] and item2['isFixed']:
                    item2['td'] = item

        # 正常表格找td
        table = browser.find_element_by_class_name('el-table__body')
        tableTr = table.find_element_by_tag_name('tbody').find_element_by_tag_name('tr')
        for index, item in enumerate(tableTr.find_elements_by_tag_name('td')):
            for item2 in tableUpdateList:
                if index == item2['index'] and not item2['isFixed']:
                    item2['td'] = item

        # 处理表格里的逻辑
        for item in tableUpdateList:
            if item['columnName'] == '存货':
                item['td'].click()
                time.sleep(1)
                browser.find_element_by_xpath(
                    "//div[contains(text(),'待入库明细')]/following-sibling::div").find_element_by_class_name(
                    'el-table__body-wrapper').find_elements_by_tag_name(
                    'td')[1].find_element_by_class_name('el-checkbox__input').click()
                time.sleep(0.5)
                browser.find_element_by_class_name('el-dialog__footer').find_elements_by_tag_name('button')[1].click()
                time.sleep(1)
            elif item['columnName'] == '货位':
                time.sleep(0.5)
                item['td'].click()
                time.sleep(0.2)
                item['td'].click()
                time.sleep(0.5)
                selectForOne(browser)
                time.sleep(0.2)
            elif item['columnName'] == '备注':
                if item['td'].find_element_by_tag_name('input').text == '':
                    item['td'].find_element_by_tag_name('input').send_keys('这是一条自动化测试数据明细')

        browser.find_element_by_xpath("//span[contains(text(),'备注:')]/following-sibling::div/input").send_keys(
            "这是一条自动化测试数据！")  # 填写备注
        time.sleep(0.2)
        browser.find_element_by_xpath("//span[contains(text(),'保存')]").click()
        time.sleep(3)
    finally:
        print(browser.current_url, '完成')
        # browser.close()
